#!/bin/bash

function push {
    msg=$1
    echo "`date` $msg" >> /var/log/kube_version.log
    token_dingtalk=`cat /opt/token_dingtalk.txt`
    curl https://oapi.dingtalk.com/robot/send?access_token=${token_dingtalk} \
            -H 'Content-Type: application/json' \
            -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"$msg\"}}"
}


current_version=`yum list kubeadm | grep x86_64 | grep -v '@' | awk '{print $2}'`
this_version="$current_version"
while true; do
    if [[ -z "$this_version" ]]; then
        echo "`date` no new version found" >> /var/log/kube_version.log
    else
        if [[ "$current_version" != "$this_version" ]]; then
            msg="kubernetes published new version $this_version"
            current_version="$this_version"
            push "$msg"
        else
            echo "`date` latest version is already installed" >> /var/log/kube_version.log
        fi
    fi
    yum makecache -y
    sleep 7200;
    this_version=`yum list kubeadm | grep x86_64 | grep -v '@' | awk '{print $2}'`
done